<?php
/**
 * Jishupu-Laravel-Vue-Admin
 * @Author:技术铺<jishupu@qq.com>
 * @Date: 2021/11/11
 */

namespace App\Http\Requests;

use App\Services\DictionaryService;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule;

/**
 * 友情链接模块表单验证
 * Class LinksRequest
 * @package App\Http\Requests
 */
class LinksRequest extends BaseRequest
{

    /**
     * 注入表单请求验证时自动校验
     *
     * @var bool
     */
    public $autoValidate = false;

    /**
     * 用户是否有权操作，true是，false否
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * 字段验证规则
     *
     * @return array
     */
    public function rules()
    {
        return [
            'id' => 'required|integer',
            'created_user_id' => 'required|integer',
            'type' => [
                'required',
                'integer',
                Rule::in(DictionaryService::getItemsValuesArray('linksTypeVar')),
            ],
            'name' => ['required', 'min:2', 'max:50'],
            'logo_image' => [
                function ($attribute, $value, $fail) {
                    if ($this->type == 2 && $value == 0) {
                        $fail(__('links.error_logo_image'));
                    }
                    AttachmentRequest::validity(__('links.logo_image'), $value, $fail, 1);
                }
            ],
            'href' => 'nullable|max:100',
            'href_target' => 'nullable|max:100',
            'rel' => 'nullable|max:20',
            'partner' => 'nullable|max:50',
            'partner_linkman' => 'nullable|max:20',
            'partner_phone_number' => 'nullable|max:20',
            'sort' => 'required|integer',
            'status' => 'required|boolean',
            'sort.*.id' => 'required|integer',
            'sort.*.sort' => 'required|integer',
        ];
    }

    /**
     * 提示信息
     * @return array
     */
    public function messages()
    {
        return [];
    }

    /**
     * 定义场景
     * @var \string[][]
     */
    public $scenes = [
        'store' => ['created_user_id', 'type', 'name', 'logo_image', 'href', 'href_target', 'rel', 'partner', 'partner_linkman', 'partner_phone_number', 'status'],
        'update' => ['type', 'name', 'logo_image', 'href', 'href_target', 'rel', 'partner', 'partner_linkman', 'partner_phone_number', 'status'],
        'destroy' => ['id'],
        'updateStatus' => ['status'],
        'updateSort' => ['sort.*.id', 'sort.*.sort']
    ];

    /**
     * 验证错误的自定义属性
     *
     * @return array
     */
    public function attributes()
    {
        return [
            'id' => __('common.id'),
            'type' => __('common.type'),
            'name' => __('common.name'),
            'logo_image' => __('links.logo_image'),
            'href' => __('common.href'),
            'href_target' => __('common.href_target'),
            'rel' => __('common.rel'),
            'partner' => __('links.partner'),
            'partner_linkman' => __('common.linkman'),
            'partner_phone_number' => __('common.phone_number'),
            'sort' => __('common.sort'),
            'status' => __('common.status'),
            'sort.*.id' => __('common.id'),
            'sort.*.sort' => __('common.sort'),
        ];
    }

    /**
     * 验证前
     *
     * @return void
     */
    protected function prepareForValidation()
    {
        $this->offsetSet('created_user_id', Auth::id());
        if (!isset($this->logo_image) || empty($this->logo_image)) $this->offsetSet('logo_image', 0);
    }
}
